package newhiant.hianto2o.com.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/8/1 0001.
 */

public class CommonAdapter<T> extends BaseAdapter {
    private Context mContext;
    private List<T> list;
    private int layoutId;
    private int variableId;// DataBinding的变量ID，可通过类似R文件的BR文件来获取

    public CommonAdapter(Context mContext, List<T> list, int layoutId, int variableId) {
        this.mContext = mContext;
        this.list = list;
        this.layoutId = layoutId;
        this.variableId = variableId;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return getItemId(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewDataBinding mViewDataBinding;
        if (view == null) {
            mViewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), layoutId, viewGroup, false);
        } else {
            mViewDataBinding = DataBindingUtil.getBinding(view);
        }
        mViewDataBinding.setVariable(variableId, list.get(i));
        return mViewDataBinding.getRoot();
    }
}
